package com.lxyk.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.lxyk.yygh.hosp.repository.DepartmentRepository;
import com.lxyk.yygh.hosp.service.DepartmentService;
import com.lxyk.yygh.model.hosp.Department;
import com.lxyk.yygh.vo.hosp.DepartmentQueryVo;
import com.lxyk.yygh.vo.hosp.DepartmentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室信息
    @Override
    public void save(Map<String, Object> paramMap) {
        //转成Department对象
        String paramString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(paramString, Department.class);
        //根据 医院编号 和 科室编号 进行查询
        Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if(targetDepartment!=null){
            //存在即更新
            targetDepartment.setUpdateTime(new Date());
            targetDepartment.setIsDeleted(0);
            departmentRepository.save(targetDepartment);
        }else{
            //不存在即新增
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.insert(department);
        }
    }

    /**
     * 查询科室信息
     * @param current
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> findPageDepartment(int current, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建Pageable对象，设置当前页和每页记录数
        //0是第一页
        PageRequest pageable = PageRequest.of(current - 1, limit);
        //vo转换成department
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        // 构建对象
        // 改变默认字符串匹配方式：模糊查询   改变默认大小写忽略方式：忽略大小写
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        // 创建Example 对象
        Example<Department> example = Example.of(department,matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    /**
     * 删除科室
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public void remove(String hoscode, String depcode) {
        //TODO 如果是手动封装department,会报错，why,mongodb
        //先查询
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     *    //排班相关展示-->根据hoscode医院编号查询所有科室
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {

        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        //查询
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        //一个医院所有科室
        List<Department> departmentList = departmentRepository.findAll(example);
        //按照bigcode进行分组
        Map<String, List<Department>> map = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //封装大科室
        for(Map.Entry<String,List<Department>> entry:map.entrySet()){
            ////大科室编号
            String bigcode = entry.getKey();
//            log.info("##########大科室编号,{}",bigcode);
            //大科室编号对应的全局数据
            List<Department> valueList = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            // TODO: 2023/7/23  医院编号和大科室编号
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(valueList.get(0).getBigname());
            //封装小科室
            ArrayList<DepartmentVo> children = new ArrayList<>();
            for(Department department:departmentList){
                if(department.getBigcode().equals(bigcode)){
                   DepartmentVo vo= new DepartmentVo();
                   vo.setDepname(department.getDepname());
                   vo.setDepcode(department.getDepcode());
                   children.add(vo);
                }
            }
          //大科室封装小科室
            departmentVo.setChildren(children);
            result.add(departmentVo);
        }

        return result;
    }

    //科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!=null){
            return department.getDepname();
        }
        return "";
    }

//    根据科室编号,和医院编号,查询科室
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }
}
